#!/usr/bin/perl

#	
#	
#	
#	
#	
#	
#	

$tmpFileName = "/tmp/buildStartTime.txt";
$logFileName = "/tmp/FMXcodeBuildLog.txt";
$now = localtime();
$verb = shift;

if ($verb =~ /^start/i) {
	#print STDERR "Logger: processing start verb\n";
	
	if (open(LOG,">$tmpFileName")) {
		$startSecs = time;
		print LOG "$startSecs\n";
	}
	else {
		print STDERR "Logger: failed to open temp file: $tmpFileName: $!\n";
	}
	
}
elsif ($verb =~ /^end/i) {	
	
	#print STDERR "Logger: processing end verb\n";
		
	if (open(LOG, $tmpFileName)) {
		$startSecs = <LOG>;
		close(LOG);
		chomp $startSecs;
		$endSecs = time;
		$elapsedSecs = $endSecs - $startSecs;
		#print "startSecs = $startSecs,  endSecs = $endSecs,  duration = $elapsedSecs\n";
		#print "target: ". $ENV{TARGET_NAME}. "  style: ". $ENV{BUILD_STYLE}. "\n";
		
		$fileCount = "";
		$fileList = $ENV{TEMP_DIR}. "/Objects-normal/". 
			$ENV{EXECUTABLE_PREFIX}. $ENV{TARGET_NAME}. ".LinkFileList";
		if (open(FILELIST, $fileList)) {
			@files = <FILELIST>;
			close(FILELIST);
			$fileCount = @files;
		}
		else {
			print STDERR "Logger: failed to open file list file: $fileList: $!\n";
		}
		
		
		if (open(LOG,">>$logFileName")) {
			$startSecs = time;
			$hours_tmp = $elapsedSecs / 3600;
			$hours = int($hours_tmp);
			$mins_tmp = ($hours_tmp - $hours) * 60;
			$mins = int($mins_tmp);
			$secs_tmp = ($mins_tmp - $mins) * 60;
			$secs = int($secs_tmp + 0.5);
			printf LOG "%s %25s %5d sec = %02d:%02d:%02d", 
				$now, "$ENV{TARGET_NAME} $ENV{BUILD_STYLE}", 
				$elapsedSecs, $hours, $mins, $secs;
			printf LOG " (%3d files ==> average %4.1f secs/file)", 
				$fileCount, $elapsedSecs / $fileCount if $fileCount;
			print LOG "\n";
			close(LOG);
		}
		else {
			print STDERR "Logger: failed to open temp file: $logFileName: $!\n";
		}
	}
	else {
		print STDERR "Logger: failed to open temp file: $tmpFileName: $!\n";
	}
}







